package com.xyu.gulimall.coupon.service.impl;


import com.xyu.common.to.CommonMemberPrice;
import com.xyu.common.to.SkuReductionTo;
import com.xyu.gulimall.coupon.entity.MemberPriceEntity;
import com.xyu.gulimall.coupon.entity.SkuLadderEntity;
import com.xyu.gulimall.coupon.service.MemberPriceService;
import com.xyu.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyu.common.utils.PageUtils;
import com.xyu.common.utils.Query;

import com.xyu.gulimall.coupon.dao.SkuFullReductionDao;
import com.xyu.gulimall.coupon.entity.SkuFullReductionEntity;
import com.xyu.gulimall.coupon.service.SkuFullReductionService;

import javax.annotation.Resource;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    private SkuLadderService skuLadderService;
    @Resource
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存满减打折，会员价
     *
     * @param reductionTo
     */
    @Override
    public void saveSkuReduction(SkuReductionTo reductionTo) {
        /**
         * sms_sku_ladder 9
         */
        //4，sku的优惠，
        //满减等信息 guilimall_sms -> sms_sku_ladder\sms_sku_full_reduction\sms_member_price
        //封装到数据表的实体类中 sms_sku_ladder表
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        //获取sku_id
        skuLadderEntity.setSkuId(reductionTo.getSkuId());
        //获取full_count 满几件
        skuLadderEntity.setFullCount(reductionTo.getFullCount());
        //获取discount 打折
        skuLadderEntity.setDiscount(reductionTo.getDiscount());
        //获取add_other 是否叠加其他优惠[0-不可叠加，1-可叠加]
        skuLadderEntity.setAddOther(reductionTo.getCountStatus());
        if (reductionTo.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);
        }
        /**
         * 满减信息  表 sms_sku_full_reduction 10
         */
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(reductionTo, skuFullReductionEntity);
        //如果满减大于0则保存
        if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0"))==1) {
            this.save(skuFullReductionEntity);
        }
        /**
         * 会员优惠 sms_member_price 11
         */
        List<CommonMemberPrice> memberPrice = reductionTo.getMemberPrice();
        List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(reductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).filter(memberPriceEntity -> {
            //过滤出大于0的说明有会员优惠进行保存
            return memberPriceEntity.getMemberPrice().compareTo(new BigDecimal("0"))==1;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(collect);
    }

}
